//
//  BaseViewController.m
//  TooToo
//
//  Created by yjyan on 13-1-7.
//  Copyright (c) 2013年 appfactory. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController()<UIGestureRecognizerDelegate>

@property (nonatomic,strong)TapActionBlock tapBlock;

@end

@implementation BaseViewController

@synthesize navigationView = _navigationView;

- (void)navback
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    //设置信息栏颜色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    [self.view bringSubviewToFront:self.navigationView];
}

- (id)init
{
    self  = [super init];
    if(self)
    {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    return self;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.frame = [[UIScreen mainScreen] bounds];
    [self.view setBackgroundColor:FBEE_COLOR_BACKGROUND];
    
    _navigationView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, NavBarHeight)];
    _navigationView.barStyle = UIBarStyleDefault;
    _navigationView.tag = TOP_NAVBAR_TAG;
    [_navigationView layoutIfNeeded];
    [self.view addSubview:_navigationView];
    
    _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(88, -2 + StatusHeight, IPHONEWIDTH-88*2, 47)];
    _titleLabel.font = [UIFont boldSystemFontOfSize:17.0f];
    _titleLabel.tag = 12305;
    _titleLabel.backgroundColor = [UIColor clearColor];
    _titleLabel.textColor = FBEE_COLOR_THEME;
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.text = @"";
    [_navigationView addSubview:_titleLabel];
    
    UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_navigationView.frame) - LINEHEIGHT, IPHONEWIDTH, LINEHEIGHT)];
    line.backgroundColor = UIColorFromRGBA(0xF8F8F8,0.82);
    line.tag = 888;
    [_navigationView addSubview:line];
    _navigationView.alpha = 0;
    
    
    UIButton *backNav =  [UIButton makeButton:^(TTButtonExtend *make) {
        make.setFrame(CGRectMake(0, StatusHeight, 60, 44));
        make.addImage([UIImage imageNamed:@"nav_back"],UIControlStateNormal);
        make.addTarget(self,@selector(navback));
        make.imageEdgeInsets = UIEdgeInsetsMake(13.5, 16, 13.5, 27);
        make.tag = 666;
        [_navigationView addSubview:make];
    }];
    
    
    _largeTitle = [[LargeTitle alloc] initWithTitle:@""];
    UIButton *backLarge =  [UIButton makeButton:^(TTButtonExtend *make) {
        make.setFrame(CGRectMake(0, StatusHeight, 60, 44));
        make.addImage([UIImage imageNamed:@"nav_back"],UIControlStateNormal);
        make.addTarget(self,@selector(navback));
        make.imageEdgeInsets = UIEdgeInsetsMake(13.5, 16, 13.5, 27);
        make.tag = 666;
        [_largeTitle addSubview:make];
    }];
    [self.view addSubview:_largeTitle];
}

-(void)hiddeBack
{
    [_navigationView viewWithTag:666].hidden = YES;
    [_largeTitle viewWithTag:666].hidden = YES;
}

- (void)bringTitleToFront
{
    [self.view bringSubviewToFront:_largeTitle];
    [self.view bringSubviewToFront:_navigationView];
    
}

-(void)addTapForView:(UIView *)view action:(TapActionBlock)block
{
    UITapGestureRecognizer * tapGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tappedViewAction)];
    [self.view addGestureRecognizer:tapGes];
    
    self.tapBlock = block;
}

-(void)tappedViewAction
{
    self.tapBlock();
}

-(void)removeNavbackButton
{
    UIView * back = [_navigationView viewWithTag:666];
    if (back) {
        [back removeFromSuperview];
    }
}

@end
